//
//  ThirdPartyLogin.swift
//  SwiftTest
//
//  Created by appleTwo on 2019/3/29.
//  Copyright © 2019 QiKai. All rights reserved.
//

import UIKit

class ThirdPartyLogin: NSObject{

    enum keyType {
        case weixin
        case qq
    }
    
    static var shareManage = ThirdPartyLogin()
    
    override init() {
        super.init()
    }
    
 
}
extension ThirdPartyLogin:UIApplicationDelegate,WXApiDelegate{
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        //向微信注册 appid
        WXApi.registerApp("")
        return true
        
    }
    //从微信端打开第三方app会调用此方法，然后此方法会调用onResp方法
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        
        let str:String = url.absoluteString
        if str.hasPrefix("微信url的前缀") {
            return WXApi.handleOpen(url, delegate: self)
        }
        if str.hasPrefix("qq的url前缀") {
            
        }
      return false
    }
    
}
extension ThirdPartyLogin{
    //*当控制器中点击微信图标时，调起微信客户端，若是第一次调起，到微信登录授权页。非首次直接微信登录
    func sendAuthRequest(viewController:UIViewController){
        if WXApi.isWXAppInstalled() {
            let weChatReq = SendAuthReq.init()
            weChatReq.scope = "snsapi_userinfo"
            weChatReq.openID = ""
            weChatReq.state = "12345"
            WXApi.sendAuthReq(weChatReq, viewController: viewController, delegate: self)
        }else{
            SVProgressHUD.showInfo(withStatus: "微信没有安装")
            SVProgressHUD.dismiss(withDelay: 0.8)
        }
    }
    
    //打开外部链接
    func open(scheme: String) {
        
        if let url = URL(string: scheme) {
            
            if #available(iOS 10, *) {
                
                UIApplication.shared.open(url, options: [:],
                                          
                                          completionHandler: {
                                            
                                            (success) in
                                            
                                            print("Open \(scheme): \(success)")
                                            
                })
                
            } else { //9.0 之前
                
                let success = UIApplication.shared.openURL(url)
                
                print("Open \(scheme): \(success)")
                
            }
            
        }else{
            
        }
        
    }
}





